package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type BcRechargeService struct {
}

// CreateBcRecharge 创建bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) CreateBcRecharge(bcRecharge *bc.BcRecharge) (err error) {
	err = global.GVA_DB.Create(bcRecharge).Error
	return err
}

// DeleteBcRecharge 删除bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) DeleteBcRecharge(bcRecharge bc.BcRecharge) (err error) {
	err = global.GVA_DB.Delete(&bcRecharge).Error
	return err
}

// DeleteBcRechargeByIds 批量删除bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) DeleteBcRechargeByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]bc.BcRecharge{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateBcRecharge 更新bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) UpdateBcRecharge(bcRecharge bc.BcRecharge) (err error) {
	err = global.GVA_DB.Save(&bcRecharge).Error
	return err
}

// GetBcRecharge 根据id获取bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) GetBcRecharge(id uint) (bcRecharge bc.BcRecharge, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bcRecharge).Error
	return
}

func (bcRechargeService *BcRechargeService) GetBcRechargeByList(ids request.IdsReq) (list []bc.BcRecharge, err error) {
	var bcRecharges []bc.BcRecharge
	err = global.GVA_DB.Where("id in ?", ids.Ids).Find(&bcRecharges).Error
	return bcRecharges, err
}

// GetBcRechargeInfoList 分页获取bcRecharge表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRechargeService *BcRechargeService) GetBcRechargeInfoList(info bcReq.BcRechargeSearch, names []string) (list []bc.BcRecharge, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&bc.BcRecharge{})
	var bcRecharges []bc.BcRecharge
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.SysUserName != "" {
		db = db.Where("sys_user_name = ?", info.SysUserName)
	}
	if len(names) > 0 {
		db = db.Where("sys_user_name in  ?", names)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&bcRecharges).Error
	return bcRecharges, total, err
}
